<?php

    use think\Db;
    use \think\Session;
/**
 * 图集的第一张图片
 * @param $content_id
 * @return mixed
 */
    function content_img_one($content_id){
        $img = Db::name('facescore_imgs')->where('content_id',$content_id)->order('id asc')->select();
        if(count($img) > 0) {
            return $img[0]['pic_path'];
        }
    }

/**
 * 图集的列表
 * @param $content_id
 * @return mixed
 */
    function content_imgs($content_id){
        $imgs = Db::name('facescore_imgs')->where('content_id',$content_id)->order('id asc')->select();
        $red_count = 0;
        foreach ($imgs as $key => $value) {
            if($value['type'] == 2) {
                $red_count ++;
            }
            //提审模式下 全部为普通照片
            if(config('module_facescore.trial_mode') == 1) {
                $imgs[$key]['type'] = 1;
            }
            # code...
        }

        //图片平拼接前缀
        $imgs = imgValues($imgs,'pic_path');
        $imgs[0]['red_count'] = $red_count;
        return $imgs;
    }

/**
 * 根据uid查询用户信息
 */
    function userInfo($uid,$value){
        $user = Db::name('admin_member')->where('id',$uid)->find();
        return $user[$value];
    }

/**
 * 给数据库中存的图片地址拼接前缀
 */
    function imgValues($data_list,$field_name){
        foreach ($data_list as $key => $value) {
            $data_list[$key][$field_name] = config('base.site_domain') . $value[$field_name];
        }
        return $data_list;
    }

/**
 * 打赏总钱数
 * @param $type
 */
    function reward_money_count($content_id,$type = '1'){
        $res = Db::name('facescore_reward')->where(['type' => $type,'content_id' => $content_id,'status' => 1])->sum('money');
        return $res;
    }

/**
 * 打赏总人数
 * @param $type
 */
    function reward_person_count($content_id,$type = '1'){
        $res = Db::name('facescore_reward')->where(['type' => $type,'content_id' => $content_id,'status' => 1])->count();
        return $res;
    }

/**
 * 小程序登录状态判断
 * @return bool
 */
    function checkLogin(){
        if(input("openid") ) {
            $count = Db::name('admin_member')->where(['openid' => input('openid'),'id' => input('uid')])->count();
            if($count > 0) {
                return true;
            }
        }
        return false;
    }
    /**
     * 小程序端处理图集列表 给图集列表的每列数据增加一个is_reward 字段，意义为是否打赏过
     * 如果 整个图集被打赏过 那么 所有的is_reward 都是 1
     * uid 查看人的uid
     */
    function deal_imgs($uid,$imgs,$content_id){
        $count = Db::name('facescore_reward')->where(['uid' => $uid,'content_id' => $content_id,'reward_type'=>'all','status' => '1'])->count();
//        echo  Db::name('facescore_reward')->getLastSql();
        //被整组打赏过
        if($count > 0) {
            foreach ($imgs as $key => $value) {
                $imgs[$key]['is_reward'] = 1;
            }
        }else{
            foreach ($imgs as $key => $value) {
                $co = Db::name('facescore_reward')->where([
                    'uid' => $uid,
                    'content_id' => $content_id,
                    'reward_type' => 'single',
                    'status' => '1',
                    'imgs_id' => $imgs[$key]['id']
                ])->count();
                if($co > 0) {
                    $imgs[$key]['is_reward'] = 1;
                }
            }
        }

        return $imgs;
    }

    /**
     * 发哦送短信相关函数
     */
    function signmd5($appId,$secretKey,$timestamp){
        return md5($appId.$secretKey.$timestamp);
    }
    function http_request($url, $data)
    {
        $data = http_build_query($data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, TRUE);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    function send_post($url, $post_data,$method='POST') {
        $postdata = $post_data;
        $options = array(
                'http' => array(
                'method' => $method, //or GET
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    function AccessToken(){
        $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('module_facescore.appid')."&secret=".config('module_facescore.secret');
        $getArr=array();
        $tokenArr=json_decode(send_post($tokenUrl,$getArr,"GET"));
        //print_r($tokenArr);
        $access_token=$tokenArr->access_token;
        return $access_token;
    }
 /**
     * 获取麦粒账户余额
     */
    function getMagBalance($mobile){
        $res = send_post(config('module_facescore.mag_api_url') . '/asset/detail/MAG/'.$mobile,'','GET');
        $res = json_decode($res);
  
        if(sizeof($res->data) > 0) {
            return round($res->data->balance,2);
        }else{
            return 0;
        }
        
    }
?>